import { PageParam } from '@/services/common'
import { covertTableData } from '@/utils/tableUtil'
import { request } from '@umijs/max'

export interface TenantVO {
  /**
   * id
   */
  id: string
  /**
   * 租户名
   */
  tenantName: string
  /**
   * 状态
   */
  status: number
  /**
   * 创建时间
   */
  createTime: string
}

export interface TenantQueryDTO extends PageParam {
  /**
   * id
   */
  id?: string
  phone?: string
}

export interface TenantUpdateDTO {
  /**
   * 管理员密码
   */
  adminPassword: string
  /**
   * 管理员手机号
   */
  adminPhone: string
  /**
   * id
   */
  id: string
  /**
   * 租户权限
   */
  permId: number[]
  /**
   * 状态
   */
  status: number
  /**
   * 租户名
   */
  tenantName: string
}

export const getTenantById = async (id: string) => {
  return request<TenantVO>(`/api/tenant?id=${id}`, { method: 'GET' })
}

export const getTenantList = async (params: TenantQueryDTO) => {
  return request<Array<TenantVO>>(`/api/tenant/list`, { method: 'GET', params })
}

export const getTenantPage = async (params: TenantQueryDTO) => {
  let response = await request<Array<TenantVO>>(`/api/tenant/page`, { method: 'GET', params })
  return covertTableData(response)
}

export const updateTenant = async (tenantUpdateDTO: TenantUpdateDTO) => {
  return request<TenantVO>(`/api/tenant/update`, { method: 'POST', data: tenantUpdateDTO })
}

export const saveTenant = async (tenantUpdateDTO: TenantUpdateDTO) => {
  return request<TenantVO>(`/api/tenant/save`, { method: 'POST', data: tenantUpdateDTO })
}

export const removeTenant = async (id: string) => {
  return request<boolean>(`/api/tenant/remove?id=${id}`, { method: 'POST' })
}

export const switchTenant = (id: string) => {
  return request<Array<TenantVO>>(`/api/tenant/switch?id=${id}`)
}
